#include "permissions.h"

#include <sys/stat.h> // umask

#include "simple/support/enum.hpp" // support::to_integer

namespace simple::io
{

	permission_flags swap_process_permission_mask(permission_flags mask) noexcept
	{
		constexpr unsigned flipper = 0b111'111'111u;
		return static_cast<permission_flags>(umask(support::to_integer(mask) ^ flipper) ^ flipper);
	}

	std::string get_message(permission_flags flags)
	{
		auto value = support::to_integer(flags);
		std::string ret = "--- --- ---";
		if(value & 0b100'000'000u) ret[0] = 'r';
		if(value & 0b010'000'000u) ret[1] = 'w';
		if(value & 0b001'000'000u) ret[2] = 'x';
		if(value & 0b000'100'000u) ret[4+0] = 'r';
		if(value & 0b000'010'000u) ret[4+1] = 'w';
		if(value & 0b000'001'000u) ret[4+2] = 'x';
		if(value & 0b000'000'100u) ret[8+0] = 'r';
		if(value & 0b000'000'010u) ret[8+1] = 'w';
		if(value & 0b000'000'001u) ret[8+2] = 'x';
		return "simple::io::permission_flags: " + ret;
	}

} // namespace simple::io
